// 此处增强设计，怎么自动定位到当前网点
const db = wx.cloud.database()
const {
  websits
} = require("../../data/websit");
import {
  getCurrenDay,
  getFullDay,
  checkIdCard,
  trim,
  isPhone
} from "../../data/common"
Page({
  /**
   * 页面的初始数据
   */
  data: {
    idCard: '', // 身份证
    name: '', //姓名
    phone: '', //电话
    workType: '', // 存钱业务
    temperature: '', // 体温
    websitName: '',
    websitCode: '',
    // 业务类型索引坐标
    index: null,
    picker: ['存钱', '取钱', '转账', '对公', '其它'],
    loadModal: false //显示加载框
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.scene) {
      var scene = decodeURIComponent(options.scene)
      var arr = scene.split("=")
      console.log("获取到的参数---->" + arr);
      this.setData({
        websitCode: arr[1]
      })

    } else {
      console.log("扫码进入页面")
      console.log("获取到的参数---->" + options.scene);
      this.setData({
        websitCode: '85080'
      })
    }

    if (wx.getStorageSync('re' + this.data.websitCode) == '') {
      var that = this;
      this.showload(true);
      db.collection('es_website').where({
        wdbm: this.data.websitCode,
      }).get().then(res => {
        console.log(res);
        var result = res.data;
        if (result.length == 0) {
          wx.showToast({
            title: '账号有误',
          })
          that.showload(false)
        } else {
          that.showload(false)
          wx.setStorageSync('re' + this.data.websitCode, result[0].wdmc);
          that.setData({
            websitName: result[0].wdmc
          })
        }

      }).catch(err => {
        that.showload(false)
      })
    } else {
      this.setData({
        websitName: wx.getStorageSync('re' + this.data.websitCode)
      })
    }
    this.initUserInfo();
  },


  /**
   * 初始化用户信息，如果用户已经填写过，
   * 自动带出上次录入个人身份信息
   */
  initUserInfo: function () {
    this.setData({
      idCard: wx.getStorageSync('idCard'),
      name: wx.getStorageSync('name'),
      phone: wx.getStorageSync('phone'),
      temperature: ''
    })
  },
  /**
   * 保存来访用户信息
   */
  saveUserInfo: function () {
    if (this.canSave()) {
      this.showload(true);
      this.saveToDb();
    }
  },
  /**
   * 检查当前用户填写信息是否完整
   * return :false-不完整  true-完整
   */
  canSave: function () {
    if (trim(this.data.idCard) == '' || !checkIdCard(this.data.idCard)) {
      return this.showMsg('身份证有误', false);
    }

    if (trim(this.data.name) == '' || this.data.name.length < 2) {
      return this.showMsg('请填写姓名', false);
    }

    if (trim(this.data.phone) == '' || !isPhone(this.data.phone)) {
      return this.showMsg('电话有误', false);
    }

    if (trim(this.data.temperature) == '' || this.data.temperature > 40 || this.data.temperature < 34) {
      return this.showMsg('请填写体温', false);
    }
    if (this.data.workType == '') {
      return this.showMsg('请选办理业务', false);
    }
    return true;
  },
  /**
   * 将客户信息保存到服务器数据库
   */
  saveToDb: function () {
    var that = this;
    db.collection('es_customer').add({
      data: {
        sfz: this.data.idCard,
        xm: this.data.name,
        dh: this.data.phone,
        ybcts: 1, //已经保存天数
        rq: getCurrenDay(),
        xxsj: getFullDay(),
        ywlx: this.data.workType,
        tw: this.data.temperature,
        wdmc: this.data.websitName,
        wdbm: this.data.websitCode
      }
    }).then(res => {
      that.showload(false);

      wx.showModal({
        title: '登记成功',
        showCancel: false,
        content:getCurrenDay()+''+"\n"+this.data.name+"\n"+this.data.idCard+''
      })
      that.saveUserInfoToLocal();
      that.initUserInfo();
    }).catch(err => {
      console.log(err);
      that.showload(false);
      that.showMsg('登记失败', false)
    })
  },
  /**
   * 将用户信息缓存在本地，用于再次进入时免填写
   */
  saveUserInfoToLocal: function () {
    console.log('保存到本地的信息')
    wx.setStorageSync('idCard', this.data.idCard);
    wx.setStorageSync('name', this.data.name);
    wx.setStorageSync('phone', this.data.phone);
  },

  /**
   * 业务类型处理
   */
  PickerChange(e) {
    console.log(e);
    this.setData({
      index: e.detail.value,
      workType: this.data.picker[e.detail.value]
    })
    console.log("worktype---" + this.data.workType)
  },
  inputidcard: function (e) {
    this.setData({
      idCard: e.detail.value
    })
  },
  inputname: function (e) {
    this.setData({
      name: e.detail.value
    })
  },
  inputphone: function (e) {
    this.setData({
      phone: e.detail.value
    })
  },
  inputtemperature: function (e) {
    this.setData({
      temperature: e.detail.value
    })
  },
  /**
   * 展示信息，返回false表示不可继续操作，返回True表示可继续操作
   */
  showMsg: function (msg, status) {
    wx.showToast({
      title: msg,
    })
    return status;
  },
  showload: function (stats) {
    this.setData({
      loadModal: stats
    })
  }
})